//
//  CameraViewController.m
//  XPhoto
//
//  Created by sentinel on 2020/5/28.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "CameraViewController.h"
#import <BKKit/NSDate+x.h>
#import "PhotoAsset.h"
#import <BKKit/UIImage+x.h>
#import "PHAsset+x.h"


@interface CameraViewController () <BKDialPlateViewDelegate, BKTabBarViewDelegate>
@property(nonatomic, assign) NSInteger mode;
@end

@implementation CameraViewController

- (void)initNavigationBar{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _mode = -1;
    self.navigationController.navigationBar.hidden = YES;
    NSArray *allModes = @[
        @{
            @"title":@"相册",
            @"mode":@CAMERA_MODE_PHOTO_LIB
        },
        @{
            @"title":@"拍照",
            @"mode":@CAMERA_MODE_TAKE_PHOTO
        },
        @{
            @"title":@"拍视频",
            @"mode":@CAMERA_MODE_SHOOT_VIDEO
        }
    ];
    NSMutableArray *items = [NSMutableArray array];
    if (self.supportModes) {
        for (NSInteger i = 0, n = allModes.count; i < n; i++) {
            NSDictionary *item = [allModes objectAtIndex:i];
            NSInteger mode = [item[@"mode"] intValue];
            for (NSNumber *m in self.supportModes) {
                if ([m intValue] == mode) {
                    [items addObject:item];
                }
            }
        }
    }
    if (!items || items.count == 0) {
        items = [NSMutableArray arrayWithArray:allModes];
    }
    self.tarBarView.items = items;
    self.tarBarView.selectedIndex = 0;
    self.mode = CAMERA_MODE_PHOTO_LIB;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (self.mode == CAMERA_MODE_TAKE_PHOTO || self.mode == CAMERA_MODE_SHOOT_VIDEO){
        [self.cameraView startSession];
    }
    
    [self.contentView setContentOffset:CGPointZero animated:YES];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    if (self.mode == CAMERA_MODE_TAKE_PHOTO || self.mode == CAMERA_MODE_SHOOT_VIDEO){
        [self.cameraView stopSession];
    }
}
- (IBAction)cancel:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)next:(UIButton*)button{
    if (self.mode == CAMERA_MODE_PHOTO_LIB) {
        [self editWithSelectedAssets];
    }
    if (self.mode == CAMERA_MODE_TAKE_PHOTO) {
        if (self.edit) {
            PhotoEditViewController *photoEditViewController = [[PhotoEditViewController alloc] initWithPhotos:self.cameraView.photos];
            photoEditViewController.delegate = self;
            [self.navigationController pushViewController:photoEditViewController animated:YES];
        }
        if (!self.edit && self.delegate) {
            [self.delegate photoLibViewController:self didSelectPhotos:self.cameraView.photos];
        }
    }
    if ( self.mode == CAMERA_MODE_SHOOT_VIDEO){
        [self.cameraView finishedWithCompletion:^(NSError * error, NSArray * photos) {
            if (!error && photos) {
                if (self.edit) {
                    PhotoEditViewController *photoEditViewController = [[PhotoEditViewController alloc] initWithPhotos:photos];
                    photoEditViewController.delegate = self;
                    [self.navigationController pushViewController:photoEditViewController animated:YES];
                }
                if (!self.edit && self.delegate) {
                    [self.delegate photoLibViewController:self didSelectPhotos:photos];
                }
            }
        }];
    }
}
- (void)updateDisplay{
    if (self.mode == CAMERA_MODE_PHOTO_LIB) {
        self.nextButton.enabled = self.selectedAssets.count > 0;
    }
    if (self.mode == CAMERA_MODE_TAKE_PHOTO) {
        self.nextButton.enabled = self.cameraView.photos.count > 0;
    }
    if (self.mode == CAMERA_MODE_SHOOT_VIDEO) {
        self.nextButton.enabled = self.cameraView.videoClips.count > 0;
    }
    self.nextButton.backgroundColor = self.nextButton.enabled ? [UIColor systemBlueColor] : [UIColor systemGrayColor];
}
/**
 *  切换模式,初始化界面
 */
- (void)setMode:(NSInteger)mode{
    if (mode == _mode) {
        return;
    }
    if (mode == CAMERA_MODE_PHOTO_LIB) {
        self.tarBarView.style = @{@"textSelectedColor":[UIColor blackColor],
                                  @"textColor":[UIColor blackColor]};
        self.tarBarView.backgroundColor = THEME_VIEW_BG_COLOR;
        [self.cameraView stopSession];
        [self.contentView setContentOffset:CGPointZero animated:YES];
    }
    if (mode == CAMERA_MODE_TAKE_PHOTO || mode == CAMERA_MODE_SHOOT_VIDEO) {
        
        self.tarBarView.style = @{@"textSelectedColor":[UIColor whiteColor],
                                  @"textColor":[UIColor whiteColor]};
        
        self.tarBarView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        self.cameraView.mode = mode;
        [self.cameraView startSession];
        [self.contentView setContentOffset:CGPointMake(self.contentView.bounds.size.width, 0) animated:YES];
    }
    _mode = mode;
    [self updateDisplay];
}
//#pragma mark - BKDialPlateViewDelegate
//- (void)dialPlateView:(BKDialPlateView *)view didSelectAtIndex:(NSInteger)index withItem:(id)item{
//    self.mode = index;
//}
#pragma mark - BKTabBarViewDelegate
- (void)tabBar:(BKTabBarView *)tabBar didSelectItem:(NSDictionary *)info{
    NSInteger mode = [info[@"mode"] intValue];
    self.mode = mode;
}
#pragma mark - CameraVeiwDelegate
- (void)cameraViewDidStartRecording:(CameraView *)view{
    self.topView.hidden = YES;
}
- (void)cameraView:(CameraView *)view didFinishRecording:(PhotoAsset *)photoAsset{
    self.topView.hidden = NO;
    [self updateDisplay];
}
- (void)cameraView:(CameraView *)view didRemoveVideo:(PhotoAsset *)photoAsset{
    [self updateDisplay];
}
- (void)cameraView:(CameraView *)view didFinishProcessingPhoto:(PhotoAsset *)photoAsset{
    [self updateDisplay];
}
- (void)cameraView:(CameraView *)view didRemovePhoto:(PhotoAsset *)photoAsset{
    [self updateDisplay];
}
- (void)cameraView:(CameraView *)view didTakePhoto:(PhotoAsset *)photoAsset{
    [self updateDisplay];
}
#pragma mark - Override super

- (void)selectedAssetsChanged{
    [super selectedAssetsChanged];
    [self updateDisplay];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
